package com.haois.component.util.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * description
 *
 * @author RuiHaoZ on GCU
 * @date 2017/3/17 9:18
 */
public class DownExcelUtil {


    private Logger logger = LoggerFactory.getLogger(LoadExcelUtil.class);

    /**
     * 下载组织用的模板
     * @param mapList
     * @param name
     * @param sheetName
     * @param head
     * @param pros
     * @return
     */
    public HSSFWorkbook create(List<Map> mapList,
                               String name, String sheetName, String[] head, String[] pros) {
        //组织树
        int depsSize = mapList.size();
        //单表头行数
        int headLength = head.length;
        //第一列
        int firstCol = 0;
        //最后一列
        int lastCol = depsSize * headLength;

        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet(sheetName);

        ExcelCellStyle cellStyle = new ExcelCellStyle(workbook);
        SetValidation validation = new SetValidation();

        HSSFPatriarch patr = sheet.createDrawingPatriarch();

        //创建批注位置
        HSSFClientAnchor anchor = patr.createAnchor(0, 0, 0, 0, 5, 1, 8, 3);

        HSSFCell cell = null;
        CellRangeAddress range;
        HSSFRichTextString text;
        HSSFRow row;
        sheet.setDefaultColumnWidth(30);
        ExcelRow excelRow = new ExcelRow(workbook);

        //第0行-表头
        excelRow.createRow(sheet, 0, name, lastCol, 500);
        //第1行-部门名
        row = sheet.createRow(1);
        for (int i = 0; i < depsSize * headLength; i = i + headLength) {
            HSSFComment comment = patr.createCellComment(anchor);
            range = new CellRangeAddress(1, 1, i, i + headLength - 1);
            //创建批注
            comment.setString(new HSSFRichTextString(String.valueOf(mapList.get(i / headLength).get("comment"))));
            //设置批注内容
            cell = row.createCell(i);

            String depName = String.valueOf(mapList.get(i / headLength).get("depName"));
            text = new HSSFRichTextString(depName);
            cell.setCellValue(text);
            cell.setCellStyle(cellStyle.alignStyle());
            cell.setCellComment(comment);
            sheet.addMergedRegion(range);
            validation.setDataValidation(sheet, cell);

        }
        //第2行-成员信息 AS 编号|姓名
        excelRow.createRowHead(sheet, head, 2, depsSize);
        //第3行到100 创建97行的空值，包含职称列
        int firstRow = 3;
        int lastRow = 100;
        int dex = 0;
        if (dex != -1) {
            for (int i = 0; i < depsSize * headLength; i++) {
                if (i % (head.length) == dex) {
                    validation.setProsValidation(sheet, cell, firstRow, lastRow, i, i, pros);
                }
            }
        }
        cellStyle.setLotCell(sheet, firstRow, lastRow, firstCol, lastCol, cellStyle.textStyle());
        return workbook;
    }

    /**
     * 下载批量注册用的模板
     * @param textString0
     * @param sheetName
     * @param head
     * @return
     */
    public HSSFWorkbook create(String textString0, String sheetName, String[] head) {

        int headLength = head.length;

        HSSFWorkbook workbook = new HSSFWorkbook();
        ExcelRow excelRow = new ExcelRow(workbook);
        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setDefaultColumnWidth(20);
        ExcelCellStyle cellStyle = new ExcelCellStyle(workbook);
        SetValidation validation = new SetValidation();

        HSSFCell cell;
        CellRangeAddress range;
        HSSFRow row;


        //第0行
        excelRow.createRow(sheet, 0, textString0, headLength, 500);
        //第1行
        excelRow.createRow(sheet, 1, "请注意保护隐私", headLength, 300);
        //第2行
        excelRow.createRowHead(sheet, head, 2);

        //设置格式为文本
        //3-99行
        int firstCol = 0;
        int lastCol = headLength;
        cellStyle.setLotCell(sheet, 3, 99, firstCol, lastCol, cellStyle.textStyle());

        return workbook;
    }

    /**
     * 下载组织成员信息表
     * @param members
     * @param name
     * @param sheetName
     * @param head
     * @return
     */
    public HSSFWorkbook createContainMsg1(String[][] members, String name, String sheetName, String[] head) {

        int strLengh = head.length;

        HSSFWorkbook workbook = new HSSFWorkbook();
        ExcelRow excelRow = new ExcelRow(workbook);

        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setDefaultColumnWidth(30);
        ExcelCellStyle cellStyle = new ExcelCellStyle(workbook);
        SetValidation validation = new SetValidation();
        sheet.setDefaultColumnWidth(20);
        HSSFCell cell;
        CellRangeAddress range;
        HSSFRow row;

        //第0行
        excelRow.createRow(sheet, 0, name, strLengh, 500);
        //第1行
        excelRow.createRow(sheet, 1, "请注意保护隐私", strLengh, 300);
        //第2行
        excelRow.createRowHead(sheet, head, 2);
        int roww = 3;
        for (int i = 0; i < members.length; i++) {
            HSSFRow rows = sheet.createRow(roww);
            for (int j = 0; j < head.length; j++) {
                rows.createCell(j).setCellValue(members[i][j]);
            }
            roww++;
        }
        return workbook;
    }

}
